package com.sgb.kjwl.view.ui.fragment.shop;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopPickBinding;
import com.sgb.kjwl.di.shopping.DaggerShoppingFragComponent;
import com.sgb.kjwl.di.shopping.ShoppingFragModule;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopInfoActivity;
import com.sgb.kjwl.view.ui.adapter.shopping.ShopPickAdapter;
import com.sgb.kjwl.viewmodel.shopping.ShopPickModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

/**
 * Author chenfangkai
 */
public class ShopPicklFragment extends AppFragment implements BasePopWindow.OnResultClick {
    public static final String KEY_TYPE = "type";
    private ShopInfoActivity activity;
    private FragmentShopPickBinding binding;
    private ShopPickAdapter adapter;
    private List<String> stringList;
    private Map map;
    @Inject
    ShopPickModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ShopInfoActivity) {
            activity = (ShopInfoActivity) context;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_pick, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
        initView();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerShoppingFragComponent.builder()
                .appComponent(appComponent)
                .shoppingFragModule(new ShoppingFragModule(this))
                .build()
                .inject(this);
    }

    public static ShopPicklFragment newInstance(String type) {
        ShopPicklFragment fragment = new ShopPicklFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        if (adapter == null) {
            adapter = new ShopPickAdapter(getActivity(), null);
        }
        /*binding.rlvList.addItemDecoration(new RecyclerCustomDecoration(0,
                0,
                0,
                getActivity().getResources().getDimension(R.dimen.dp5), getActivity().getResources().getColor(R.color.color_EDEDED)));*/
        binding.rlvList.setLayoutManager(layoutManager);
        binding.rlvList.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {

            }
        });

        binding.rlvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.refresh();
            }

            @Override
            public void onLoadMore() {
                viewModel.lodaMore();
            }
        });

        adapter.setDatas(stringList);
    }


    private void init() {
        map = new HashMap();
        stringList = new ArrayList<>();
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
    }


    public void refresh(List<String> stringList) {
        binding.emptyView.setVisibility(View.GONE);
        binding.rlvList.setVisibility(View.VISIBLE);
        binding.rlvList.setLoadingMoreEnabled(true);
        adapter.setDatas(stringList);
        binding.rlvList.refreshComplete();
        if (adapter.getItemCount() >= stringList.size()) {
            binding.rlvList.setLoadingMoreEnabled(true);
        }
    }


    public void loadMore(List<String> stringList) {
        adapter.addDatas(stringList);
        binding.rlvList.loadMoreComplete();
        if (adapter.getItemCount() >= stringList.size()) {
            binding.rlvList.setLoadingMoreEnabled(true);
        }
    }

    public void empty() {
        binding.rlvList.refreshComplete();
        binding.rlvList.setVisibility(View.GONE);
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    public void loadMoreComplete() {
        binding.rlvList.loadMoreComplete();
    }

    @Override
    public void result(Object key1, Object key2,Object key3) {
        List<String> noList = (List<String>) key2;
        map.put("city", (String) key1);
        map.put("attributes", noList);
        viewModel.refresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
